package com.atguigu.dao;

import com.atguigu.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Dao中通用方法 增删改查
 * @Author luoyin
 * @Date 19:27 2022/7/15
 **/
public class BeanDao<T> {
    //增加 删除 修改 通用方法
    public int update(String sql,  Object... args){
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            int update = new QueryRunner().update(
                    connection,
                    sql,
                    args
            );

            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //JDBCUtils.release(connection,null);
        }
        return -100;
    }

    //查询一条记录
    public T selectOne(Class<T> clazz , String sql , Object... args){
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            T t = new QueryRunner().query(
                    connection,
                    sql,
                    new BeanHandler<T>(clazz),
                    args
            );
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //JDBCUtils.release(connection,null);
        }
        return null;
    }

    //查询多条记录
    public List<T> selectList(Class<T> clazz , String sql , Object... args){
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            List<T> list = new QueryRunner().query(
                    connection,
                    sql,
                    new BeanListHandler<>(clazz),
                    args
            );
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //JDBCUtils.release(connection,null);
        }
        return null;
    }

    //查询数量
    public Long selectCount(String sql , Object... args){
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return Long.valueOf(new QueryRunner().query(
                    connection,
                    sql,
                    new ScalarHandler<>(),
                    args
            ).toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //JDBCUtils.release(connection,null);
        }
        return -100L;
    }

    //添加并返回自增长键值
    public Integer insert(String sql , Object... args){
        Connection connection = null;
        try{
            connection =  JDBCUtils.getConnection();
            Integer insert = new QueryRunner().insert(
                    connection,
                    sql,
                    new ResultSetHandler<Integer>() {
                        @Override
                        public Integer handle(ResultSet resultSet) throws SQLException {
                            Integer orderId = null;
                            if (resultSet.next()) {
                                orderId = resultSet.getInt(1);
                            }
                            return orderId;
                        }
                    },
                    args
            );
            return insert;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //JDBCUtils.release(connection,null);
        }
        return null;
    }

    //批量操作
    public void batch(String sql , Object[][] orderItemParamsArr){
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            new QueryRunner().batch(
                    connection,
                    sql,
                    orderItemParamsArr
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //JDBCUtils.release(connection,null);
        }

    }

}
